package com.phpxiu.app.adapter;

import android.content.Context;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;

import com.phpxiu.app.adapter.holder.RoomGiftViewHolder;
import com.phpxiu.app.kkylive.R;
import com.phpxiu.app.model.gift.Gift;
import com.phpxiu.app.utils.KKYUtil;
import com.phpxiu.app.view.custom.VerticalImageSpan;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by WuJinZhou on 2016/5/25.
 */
public class RoomGiftGridAdapter extends BaseAdapter {
    public final static String TAG="RoomGiftGridAdapter";
    private List<Gift>pager=new ArrayList<Gift>();
    private LayoutInflater mInflater;
    private VerticalImageSpan priceIconSpan;
    private String expStr="";
    public RoomGiftGridAdapter(Context context, List<Gift> pager) {
        this.pager = pager;
        mInflater=LayoutInflater.from(context);
        expStr=context.getString(R.string.gift_exp_title);
        int size= KKYUtil.dip2px(context, 12);
        priceIconSpan=new VerticalImageSpan(context, KKYUtil.getBitmapByDrawable(context, R.mipmap.diamond_icon, size, size));
    }

    @Override
    public int getCount() {
        return pager.size();
    }

    @Override
    public Object getItem(int position) {
        return pager.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        RoomGiftViewHolder holder=null;
        Gift gift=pager.get(position);
        if(convertView==null){
            convertView=mInflater.inflate(R.layout.main_room_window_gift_grid_item_view,null);
            holder=new RoomGiftViewHolder(convertView,priceIconSpan);
            convertView.setTag(holder);
        }else{
            holder=(RoomGiftViewHolder)convertView.getTag();
        }
        holder.setGift(gift);
        holder.icon.setImageURI(Uri.parse(gift.getIcon()));
        if("1".equals(gift.getCombo())){
            if(gift.isSelected()){
                //选中状态
                holder.checkIcon.setImageResource(R.mipmap.gift_item_selected_icon);
            }else{
                holder.checkIcon.setImageResource(R.mipmap.gift_item_normal_icon);
            }
            if(holder.checkIcon.getVisibility()!=View.VISIBLE){
                holder.checkIcon.setVisibility(View.VISIBLE);
            }
        }else{
            if(holder.checkIcon.getVisibility()==View.VISIBLE){
                holder.checkIcon.setVisibility(View.INVISIBLE);
            }
        }
        holder.updatePrice(gift.getPrice());
        holder.description.setText("+"+ gift.getExp()+expStr);
        return convertView;
    }

    /**
     * 刷新状态
     */
    public void refreshState(Gift gift){
        boolean isRefresh=false;
        for (Gift g:pager){
            if(gift.getId().equals(g.getId())){
                if(!g.isSelected()){
                    g.setSelected(true);
                }else{
                    g.setSelected(false);
                }
                isRefresh=true;
            }
            if(!gift.getId().equals(g.getId())&&g.isSelected()){
                g.setSelected(false);
                isRefresh=true;
            }
        }
        if(isRefresh){
            notifyDataSetChanged();
        }
    }
}
